package com.example.test.jump;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.example.common.jump.ActivityStruct;
import com.example.common.jump.IntentKeys;

/**
 * Activity跳转工具类
 */

public class ActivityForwardUtils {
	public static final String TAG = "ActicityForwardUtils";
	private final static ActivityForwardUtils INSTANCE = new ActivityForwardUtils();
	private Class<? extends Activity> lastActivity;    		//记录最后的页面
	private static final long SPAN_TIME = 1000;            //两次跳转的间隔时间
	private long lastForwardTime = 0;                      //最后的跳转时间

	private ActivityForwardUtils() {

	}

	public static ActivityForwardUtils getInstance() {
		return ActivityForwardUtils.INSTANCE;
	}

	/**
	 * 最基本的跳转方法
	 *
	 * @param fromActivity 从哪个页面跳转
	 * @param toActivityId 目标Id
	 */
	public void forward(Activity fromActivity, int toActivityId) {
		forward(fromActivity, toActivityId, null, null, false);
	}


	/**
	 * 带数据的跳转方法
	 *
	 * @param fromActivity 从哪个页面跳转
	 * @param toActivityId 目标Id
	 * @param intent       数据
	 */
	public void forward(Activity fromActivity, int toActivityId, Intent intent) {
		forward(fromActivity, toActivityId, intent, null, false);
	}


	/**
	 * 带数据的跳转方法
	 *
	 * @param fromActivity 从哪个页面跳转
	 * @param toActivityId 目标Id
	 * @param intent       数据
	 * @param isResult     是否有返回值
	 */
	public void forward(Activity fromActivity, int toActivityId, Intent intent, boolean isResult) {
		forward(fromActivity, toActivityId, intent, null, isResult);
	}

	/**
	 * 带数据的跳转方法
	 *
	 * @param fromActivity 从哪个页面跳转
	 * @param toActivityId 目标Id
	 * @param intent       数据
	 * @param animators    跳转动画
	 */
	public void forward(Activity fromActivity, int toActivityId, Intent intent, int[] animators) {
		forward(fromActivity, toActivityId, intent, animators, false);
	}

	/**
	 * 最终的跳转方法
	 *
	 * @param fromActivity 从哪个页面跳转
	 * @param toActivityId 目标Id
	 * @param intent       数据
	 * @param animators    动画数组
	 * @param isResult     是否有返回值
	 */
	public void forward(Activity fromActivity, int toActivityId, Intent intent, int[] animators, boolean isResult) {
		ActivityStruct activityStruct = ActivityMapping.getinstance().getActivityStruct(toActivityId);
		Class<? extends Activity> toActivity = activityStruct.getClassName();
		if (lastActivity != null && lastActivity == toActivity && System.currentTimeMillis() - lastForwardTime < SPAN_TIME) {
			return;
		}
		lastActivity = toActivity;
		lastForwardTime = System.currentTimeMillis();

		if (null == intent) {
			intent = new Intent();
		}
		if (TextUtils.isEmpty(intent.getStringExtra(IntentKeys.TITLE_NAME))) {
			intent.putExtra(IntentKeys.TITLE_NAME, activityStruct.getTitle());
		}
		intent.setClass(fromActivity, toActivity);
		if (isResult) {
			fromActivity.startActivityForResult(intent, 0);
		} else {
			fromActivity.startActivity(intent);
		}
		if (null != animators) {
			fromActivity.overridePendingTransition(animators[0], animators[1]);
		}

	}


	//获取最顶上的Activity
	public Class<? extends Activity> getLastActivity() {
		return lastActivity;
	}
}
